package com.ruoyi.web.controller.system;

import java.util.ArrayList;
import java.util.List;

import com.ruoyi.system.domain.TPatient;
import com.ruoyi.system.domain.TWard;
import com.ruoyi.system.domain.vo.TTransferLogVo;
import com.ruoyi.system.service.ITPatientService;
import com.ruoyi.system.service.ITWardService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.TTransferLog;
import com.ruoyi.system.service.ITTransferLogService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

import javax.annotation.Resource;

/**
 * 寻访日志Controller
 *
 * @author 杨一迪
 * @date 2024-12-29
 */
@Controller
@RequestMapping("/system/log")
public class TTransferLogController extends BaseController {
    private String prefix = "system/log";

    @Autowired
    private ITTransferLogService tTransferLogService;

    @Resource
    private ITPatientService patientService;

    @Resource
    private ITWardService wardService;

    @RequiresPermissions("system:log:view")
    @GetMapping()
    public String log() {
        return prefix + "/log";
    }

    /**
     * 查询寻访日志列表
     */
    @RequiresPermissions("system:log:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(TTransferLog tTransferLog) {
        startPage();
        List<TTransferLog> list = tTransferLogService.selectTTransferLogList(tTransferLog);
        List<TTransferLogVo> tTransferLogVos = new ArrayList();
        list.forEach(tfl -> {
            TTransferLogVo tTransferLogVo = new TTransferLogVo();
            BeanUtils.copyProperties(tfl, tTransferLogVo);
            TWard tWard = wardService.selectTWardById(tfl.getWardId());
            TPatient tPatient = patientService.selectTPatientById(tfl.getPatientId());
            tTransferLogVo.setPatientName(tPatient.getName());
            tTransferLogVo.setWardName(tWard.getWardNumber());
            tTransferLogVos.add(tTransferLogVo);
        });
        return getDataTable(tTransferLogVos);
    }

    /**
     * 导出寻访日志列表
     */
    @RequiresPermissions("system:log:export")
    @Log(title = "寻访日志", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(TTransferLog tTransferLog) {
        List<TTransferLog> list = tTransferLogService.selectTTransferLogList(tTransferLog);
        ExcelUtil<TTransferLog> util = new ExcelUtil<TTransferLog>(TTransferLog.class);
        return util.exportExcel(list, "寻访日志数据");
    }

    /**
     * 新增寻访日志
     */
    @GetMapping("/add")
    public String add(ModelMap mmap) {
        List<TPatient> tPatients = patientService.selectTPatientList(new TPatient());
        List<TWard> tWards = wardService.selectTWardList(new TWard());
        mmap.put("tPatients", tPatients);
        mmap.put("tWards", tWards);
        return prefix + "/add";
    }

    /**
     * 新增保存寻访日志
     */
    @RequiresPermissions("system:log:add")
    @Log(title = "寻访日志", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(TTransferLog tTransferLog) {
        return toAjax(tTransferLogService.insertTTransferLog(tTransferLog));
    }

    /**
     * 修改寻访日志
     */
    @RequiresPermissions("system:log:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap) {
        TTransferLog tTransferLog = tTransferLogService.selectTTransferLogById(id);
        mmap.put("tTransferLog", tTransferLog);
        List<TPatient> tPatients = patientService.selectTPatientList(new TPatient());
        List<TWard> tWards = wardService.selectTWardList(new TWard());
        mmap.put("tPatients", tPatients);
        mmap.put("tWards", tWards);
        return prefix + "/edit";
    }

    /**
     * 修改保存寻访日志
     */
    @RequiresPermissions("system:log:edit")
    @Log(title = "寻访日志", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(TTransferLog tTransferLog) {
        return toAjax(tTransferLogService.updateTTransferLog(tTransferLog));
    }

    /**
     * 删除寻访日志
     */
    @RequiresPermissions("system:log:remove")
    @Log(title = "寻访日志", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(tTransferLogService.softDeleteTTransferLogByIds(ids));
    }
}
